package models

const (
	// resource
	ResourceStatusNormal = 0
	ResourceStatusDelete = -1

	// dag
	DagStatusNormal = 0
	DagStatusDelete = -1

	// processor
	ProcessorStatusNormal = int(0)
	ProcessorStatusDelete = int(-1)

	ProcessStateInit    = 0
	ProcessStateReady   = 100
	ProcessStateRunning = 200
	ProcessStateSuccess = 400
	ProcessStateFail    = 500
	ProcessStateError   = 600
	ProcessStateWait    = 800

	// dispatch
	DispatchEventResourceAdd      = "resource_add"
	DispatchEventProcessorDone    = "processor_done" // 任务完成包含任务成功和失败
	DispatchEventBusinessCallback = "business_callback"

	// business
	BusinessEventName = "business_event"

	// process
	ProcessTypeExecute  = "execute"
	ProcessTypeCallback = "callback"
)

type DispatchEvent struct {
	// 初始化变量
	Event       string `json:"event"`
	ResourceId  int64  `json:"resource_id"`
	DagId       int    `json:"dag_id"`
	ProcessorId int    `json:"processor_id"`
	Result      int64  `json:"result"`
	// 中间计算使用变量
	Resource             *Resource             `json:"-"`
	Dag                  *Dag                  `json:"-"`
	ResourceProcessState *ResourceProcessState `json:"-"`
	Graph                Graph                 `json:"-"`
	// 调度结果
	ExecutorList []int64 `json:"-"`
}

type ProcessEvent struct {
	ResourceId  int64  `json:"resource_id"`
	ProcessorId int    `json:"processor_id"`
	ProcessType string `json:"process_type"`
	Result      int64  `json:"result"`
	// 处理中间变量
	Resource             *Resource             `json:"-"`
	Processor            *Processor            `json:"-"`
	ResourceProcessState *ResourceProcessState `json:"-"`
	// 处理结果
	ProcessMsg   string `json:"-"`
	ProcessCnt   int    `json:"-"`
	ProcessState int    `json:"-"`
}

type BusinessEvent struct {
	Event       string `json:"event"`
	ResourceId  int64  `json:"resource_id"`
	ProcessorId int    `json:"processor_id"`
}

type CallbackEvent struct {
	ResourceId  int64 `json:"resource_id"`
	ProcessorId int   `json:"processor_id"`
	Result      int64 `json:"result"`
}

type DagConfig struct {
	Processors []int   `json:"processors"`
	Relations  [][]int `json:"relations"`
}

type Node struct {
	ProcessorId int
	Parents     []*Node
	Children    []*Node
}

type Graph map[int]*Node

func (d Graph) AddNode(processorId int) {
	d[processorId] = &Node{ProcessorId: processorId}
}

func (d Graph) AddEdge(relation []int) {
	from := relation[0]
	to := relation[1]
	d[from].Children = append(d[from].Children, d[to])
	d[to].Parents = append(d[to].Parents, d[from])
}
